package com.wc.alorithm_luogu._贪心.Poker_Hands_S;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/10 16:21
 * @description https://www.luogu.com.cn/problem/P3078
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[] a = new int[N];
    static int[] f = new int[N << 2];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        // 和把所有树都砍成平的，如果后面的比前面的少，那就说明前面的把后面的带过了
        // 后面的比前面的多，那就说明这个得多砍比前面多多少的树
        long res = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] > a[i - 1]) res += a[i] - a[i - 1];
        }
//        int j = 0;
//        for (int i = 1; i <= n; i++) {
//            if (a[i] != a[j]) a[++j] = a[i];
//        }
//        n = j;
//        build(1, n, 1);
//        out.println(bs(1, n, 0));
        out.println(res);
        out.flush();
    }

    static long bs(int l, int r, int last) {
        if (l > r) return 0;
        long res = 0;
        int minIdx = getMinIdx(1, n, 1, l, r);
        res += a[minIdx] - last;
        res += bs(l, minIdx - 1, a[minIdx]);
        res += bs(minIdx + 1, r, a[minIdx]);
        return res;
    }

    static int getMinIdx(int l, int r, int t, int ql, int qr) {
        if (l == ql && r == qr) {
            return f[t];
        }
        int mid = l + r >> 1;
        if (mid >= qr) return getMinIdx(l, mid, t << 1, ql, qr);
        else if (mid + 1 <= ql) return getMinIdx(mid + 1, r, t << 1 | 1, ql, qr);
        int left = getMinIdx(l, mid, t << 1, ql, mid);
        int right = getMinIdx(mid + 1, r, t << 1 | 1, mid + 1, qr);
        if (a[left] < a[right]) return left;
        return right;
    }

    static void build(int l, int r, int t) {
        if (l == r) {
            f[t] = l;
            return;
        }
        int mid = l + r >> 1;
        build(l, mid, t << 1);
        build(mid + 1, r, t << 1 | 1);
        f[t] = a[f[t << 1]] < a[f[t << 1 | 1]] ? f[t << 1] : f[t << 1 | 1];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}